package com.honglang.rss.impl;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class XmlEventWriter {
	
	private String  xmlFile;
	private XMLOutputFactory xmlOutputFactory;
	private XMLEventFactory xmlEventFactory;
	private XMLEventWriter xmlEventWriter;
	private XMLEvent end ;
	private XMLEvent tab ;
	private String encoding;
	
	
	public XmlEventWriter(String file,String encoding) throws FileNotFoundException, XMLStreamException{
		this.setXmlOutputFactory(XMLOutputFactory.newInstance());
		this.setXmlEventFactory(XMLEventFactory.newInstance());
		this.setXmlFile(file);
		this.encoding = encoding;
		this.setXmlEventWriter(this.getXmlOutputFactory()
				.createXMLEventWriter(new FileOutputStream(xmlFile),encoding));
		this.setEnd(this.getXmlEventFactory().createDTD("\n"));
		this.setTab(this.getXmlEventFactory().createDTD("\t"));
	}

	
	

	
	public void startDocument(String encoding) throws XMLStreamException{
			StartDocument startDocument = this.getXmlEventFactory().createStartDocument(encoding);
			this.getXmlEventWriter().add(startDocument);
	}
	
	
	
	public void endDocument() throws XMLStreamException{
		this.getXmlEventWriter().add(this.getXmlEventFactory().createEndDocument());
		this.getXmlEventWriter().close();
	}
	
	
	
	/**
	 * 
	 * @param name
	 * @param value
	 * @param tabCount number of  Tab Indentation 
	 * @throws XMLStreamException
	 * @throws UnsupportedEncodingException 
	 */
	protected void createNode(String name,String value,int tabCount) throws XMLStreamException, UnsupportedEncodingException{
		// Create Start node
		
		for(int i=tabCount;i>0;i--){
			this.getXmlEventWriter().add(this.getTab());
		}
		
		StartElement sElement = this.getXmlEventFactory().createStartElement("", "", name);
		this.getXmlEventWriter().add(sElement);
		// Create Content
		
		Characters characters =  this.getXmlEventFactory().createCharacters(value);
		//Characters characters = this.getXmlEventFactory().createCData(value);
		this.getXmlEventWriter().add(characters);
		// Create End node
		EndElement eElement =  this.getXmlEventFactory().createEndElement("", "", name);
		this.getXmlEventWriter().add(eElement);
		this.getXmlEventWriter().add(this.getEnd());
	}
	
	
	
	
	
	
	
	
	
	
	
	protected void setXmlFile(String xmlFile) {
		this.xmlFile = xmlFile;
	}
	
	
	
	protected XMLOutputFactory getXmlOutputFactory() {
		return xmlOutputFactory;
	}


	protected void setXmlOutputFactory(XMLOutputFactory xmlOutputFactory) {
		this.xmlOutputFactory = xmlOutputFactory;
	}

	protected XMLEventFactory getXmlEventFactory() {
		return xmlEventFactory;
	}


	protected void setXmlEventFactory(XMLEventFactory xmlEventFactory) {
		this.xmlEventFactory = xmlEventFactory;
	}
	
	
	

	protected XMLEventWriter getXmlEventWriter() {
		return xmlEventWriter;
	}


	protected void setXmlEventWriter(XMLEventWriter xmlEventWriter) {
		this.xmlEventWriter = xmlEventWriter;
	}


	protected String getXmlFile() {
		return xmlFile;
	}


	protected XMLEvent getEnd() {
		return end;
	}


	protected void setEnd(XMLEvent end) {
		this.end = end;
	}


	protected XMLEvent getTab() {
		return tab;
	}			

	protected void setTab(XMLEvent tab) {
		this.tab = tab;
	}





	protected String getEncoding() {
		return encoding;
	}





	protected void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	
	
}
